import config from '@/static/config.js'
function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatDate(date) {
  var date = new Date(date);
  var YY = date.getFullYear() + '-';
  var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  return YY + MM + DD +" "+hh + mm + ss;
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function(milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function(dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};

function checkNeedLogin() {
	if (getToken() == null) {
		uni.switchTab({
			url: '/pages/index/index'
		});
		return null
	}
	return getToken()
}

function getToken() {
	var token_obj = null
	uni.getStorage({
		key: 'LOGIN_TOKEN',
		success: function(res) {
			if (res.data != null) {
				token_obj = res.data
			}
		}
	})
	return token_obj
}

function logout() {
	uni.removeStorage({
		key: 'LOGIN_TOKEN',
		success: function(res) {}
	})
	uni.switchTab({
		url: '/pages/index/index'
	});
}

function requestAPI(url, method = 'POST', data, loading_text = null, successCall = null, failedCall = null, completeCall = null) {
	if (loading_text != null) {
		uni.showLoading({
			title: loading_text
		});
	}
	uni.request({
		url: url,
		method: method,
		data: data,
		dataType: 'json',
		success: function(res) {
			if (res.data.code != 'OK') {
				if (res.data.msg == 'token is invalid' || res.data.msg == 'session not exist' || res.data.msg == 'session expire') {
					logout()
				}
				uni.showToast({
					icon: 'none',
					title: res.data.msg
				});
				return false
			}
			if (successCall != null) {
				successCall(res.data)
			}
		},
		fail: function(res) {
			uni.showToast({
				icon: 'none',
				title: '请求失败'
			});
			if (failedCall != null) {
				failedCall(res.data)
			}
		},
		complete: function(res) {
			if (loading_text != null) {
				uni.hideLoading()
			}
			if (completeCall != null) {
				completeCall(res.data)
			}
		}
	})
}

module.exports = {
	formatTime: formatTime,
	formatDate: formatDate,
	formatLocation: formatLocation,
	dateUtils: dateUtils,
	checkNeedLogin: checkNeedLogin,
	getToken: getToken,
	logout: logout,
	requestAPI: requestAPI,
	config:config
}
